<?php

// 获取活动详情
// GET activity.get

namespace app\api\resource\activity;
use think\Db;
use think\Session;

class Get
{
    // 方法
    public $_method = ['GET'];
    // 前置
    public $_pre    = [];
    // 描述
    public $_description = '获取活动详情';
    // 参数
    public $_param  = [
        'id' => '活动主键',
    ];
    // 返回
    public $_return = [
        200 => '获取成功',
        422 => '活动主键必须',
        404 => '活动不存在',
    ];

    public function run(&$request)
    {
        $id = $request->get('id/d',0);

        if (0 == $id) return [422,'活动主键必须'];
        
        $activity = Db::name('activity')->where('id',$id)->find();

        if ( is_null($activity) ) return [404,'活动不存在'];

        if ( 0 == $activity['type'] ) {
            if ( Session::has('member') ) {
                // 是否报名
                $memberId = Session::get('member.id');
                $ret = Db::name('activity_signup')->where('activity_id',$id)->where('member_id',$memberId)->find();
                $activity['is_sign_up'] = is_null($ret) ? 0 : 1;
            }
            // 报名统计&列表
            $activity['sign_up_count']   = Db::name('activity_signup')->where('activity_id',$id)->count();
            $activity['sign_top10_list'] = Db::name('activity_signup')->where('activity_id',$id)->order('create_time DESC')->limit(10)->select();
        }

        return [200,$activity];
    }
}